import {
  api
} from '../../libs/api'
import {
  mapState,
  mapActions
} from "vuex";
import Vue from 'vue'
import axios from 'axios'
var config = {
  baseURL: `http://192.168.21.213:8080`,
  withCredentials: true,
  timeout: 5000
}
export function getProductInfo(ctx) { 
  api.onRequest('$method.$get.sys.product', null, null, null, function (msg, reply) {
    var res = JSON.parse(msg)
    if (res && res.code == 0 && res.data) {  
      ctx.$store.dispatch("system/getProductInfo", res.data);
      if(res.data.modelNameId == 'zhishen-104E' || res.data.modelNameId.toUpperCase() == 'MCU-1090'){
        ctx.$store.dispatch("kvm/hideKvm", true);//隐藏KVM菜单
      }
      if(res.data.modelNameId.toUpperCase() === "MCU-7100") {
        ctx.$store.commit("system/getspecialtype", true);
      }
    } else {
      ctx.$store.dispatch("system/getProductInfo", '');
    }
  }.bind(this))

  //是否隐藏KVM菜单
  ctx.$store.dispatch("kvm/hideKvm", true);
  api.onRequest('$method.$get.kvm.seat.cfg', null, null, null, function (msg, reply) {
    var res = JSON.parse(msg)
    if (res && res.code == 0 && res.data) {  
      ctx.$store.dispatch("kvm/hideKvm", false);
    }
  }.bind(this))

  //是否隐藏Multicast菜单
  api.onRequest('$method.$get.stream.transfer.multicast', null, null, null, function (msg, reply) {
    var res = JSON.parse(msg)
    if (res && res.code == 0 && res.data) {  
      ctx.$store.dispatch("media/hideMulticast", false);
    } 
  }.bind(this))
}

export function getSystemTime(ctx) {
  api.onRequest('$method.$get.sys.datetime', null, null, null, function (msg, reply) {
    var res = JSON.parse(msg)
    if (res && res.code == 0 && res.data && res.data.datetime) {
      ctx.$store.dispatch("system/getSystemTime", res.data.datetime);
    } else {
      ctx.$store.dispatch("system/getSystemTime", '');
    }
  })
}

export function autoFitTime(ctx) {
  api.onRequest('$method.$get.sys.ntpServer', null, null, null, function (msg, reply) {
    var res = JSON.parse(msg)
    if (res && res.code == 0 && res.data) {
      ctx.$store.dispatch("system/autoFitTime", res.data);
    } else {
      ctx.$store.dispatch("system/autoFitTime", '');
    }
  })
}
//硬盘信息
export function getDiskInfo(ctx) {
  api.onRequest('$method.$get.sys.storage', null, null, null, function (msg, reply) {
    var res = JSON.parse(msg)
    if (res && res.code == 0 && res.data && res.data.diskCnt) {
      ctx.$store.dispatch("system/getDiskInfo", '');
      ctx.$store.dispatch("system/getDiskCnt", res.data.diskCnt);
      for (var i = 0; i < res.data.diskCnt; i++) {
        (function (i) {
          api.onRequest('$method.$get.sys.storage.disk' + i + '.info', null, null, null, function (message, reqReply) {
            var data = JSON.parse(message);
            if (data && data.code == 0 && data.data && data.data) {
              data.data.index = i
              ctx.$store.dispatch("system/getDiskInfo", data.data);
            }
          }.bind(this))
        })(i)


      }
    } else {
      ctx.$store.dispatch("system/getDiskCnt", 0);
    }
  })
}

export function getNetsInfo(ctx) {

  api.onRequest('$method.$get.sys.net', null, null, null, function (msg, reply) {
    var res = JSON.parse(msg)
    if (res && res.code == 0 && res.data && res.data.lanCnt) {
      // ctx.$store.dispatch("system/getNetsInfo", '');
      ctx.$store.dispatch("system/getNetsCnt", res.data.lanCnt);
      for (var i = 0; i < res.data.lanCnt; i++) {
        (function (i) {
          //获取网络配置（get、set）
          api.onRequest('$method.$get.sys.net.lan' + i + '.cfg', null, null, null, function (message, reqReply) {
            var data = JSON.parse(message);
            if (data && data.code == 0 && data.data && data.data) {
              data.data.index = i
              ctx.$store.dispatch("system/getNetsInfo", data.data);
            }
          })
          //获取网络信息（get）
          api.onRequest('$method.$get.sys.net.lan' + i + '.info', null, null, null, function (message, reqReply) {
            var data = JSON.parse(message);
            if (data && data.code == 0 && data.data) {
              var req = {
                index: i,
                info: data.data
              }
              ctx.$store.dispatch("system/getNetsStatus", req);
            }
          })
        }(i))
      }
    } else {
      ctx.$store.dispatch("system/getDiskCnt", '');
    }
  }.bind(this))
}
export function getNetsState(ctx) {

  api.onRequest('$method.$get.sys.net', null, null, null, function (msg, reply) {
    var res = JSON.parse(msg)
    if (res && res.code == 0 && res.data && res.data.lanCnt) {
      // ctx.$store.dispatch("system/getNetsInfo", '');
      ctx.$store.dispatch("system/getNetsCnt", res.data.lanCnt);
      for (var i = 0; i < res.data.lanCnt; i++) {
        (function (i) {        
          //获取网络信息（get）
          api.onRequest('$method.$get.sys.net.lan' + i + '.info', null, null, null, function (message, reqReply) {
            var data = JSON.parse(message);
            if (data && data.code == 0 && data.data) {
              var req = {
                index: i,
                info: data.data
              }
              ctx.$store.dispatch("system/getNetsStatus", req);
            }
          })
        }(i))
      }
    } else {
      ctx.$store.dispatch("system/getDiskCnt", '');
    }
  }.bind(this))
}

export function getAgingStates(ctx){
  
  api.onRequest('$method.$get.sys.aging', null, null, null, function (msg, reply) {
    var res = JSON.parse(msg)
    if (res && res.code == 0 && res.data) {
      ctx.$store.dispatch("system/getAgingStates", res.data);
    } else {
      ctx.$store.dispatch("system/getAgingStates", '');
    }
  })
}

export function getTopics(ctx){
  var itemPages = 4
  var req = {
    offset: 95
  }
  ctx.$store.dispatch("system/getTopics", []);   
  api.onRequest('$method.$query.$topic', JSON.stringify(req), null, null,  (msg, reply) =>{
    var res = JSON.parse(msg)
    if (res && res.code == 0 && res.data) {
      itemPages = Math.ceil(res.data.total / 32)
      ctx.$store.dispatch("system/getTopics", res.data.items);
      for(var i = 1; i <= itemPages; i++){
        (function(i){
          var offset = i * 32 -1
          api.onRequest('$method.$query.$topic', JSON.stringify({offset: offset}), null, null,  (msg1, reply1) =>{     
            var res1 = JSON.parse(msg1)   
            if (res1 && res1.code == 0 && res1.data) {
              for(var item of res1.data.items){
                if(item.topicId.indexOf('media.mi.dev') > -1 && item.topicId.indexOf('loopback') > -1){                  
                  var devIndex = parseInt(item.topicId.substr(12, 1))
                  var req = {};
                  req.index = devIndex;
                  req.enabled = true
                  ctx.$store.dispatch("media/getInputDevInfo", req);
                }
              }
              //loop显示问题
            }
          })
        }(i))       
      }
      
      
    }
  })
}